<?php $this->load->view('admin/Public/header'); ?>
<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">地区名称</label>
					<div class="layui-input-inline">
						<input type="text" id="name" name="name" placeholder="请输入名称" autocomplete="off" class="layui-input">
					</div>
				</div>

				<button class="layui-btn" data-type="reload">搜索</button>
			</div>
		</div>

		<div class="layui-card-body">
			<script type="text/html" id="toolbarDemo">
				<div class="layui-btn-container">
					<button class="layui-btn layui-btn-sm" lay-event="history" id="back" style="display: none">上一级</button>
				</div>
			</script>
			<script type="text/html" id="barDemo2">
				<div class="layui-btn-container">
					{{#  if(d.level != 3){ }}
					<button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="son">管理子地区</button>
					{{#  } else { }}

					{{#  } }}
				</div>
			</script>
			<table id="LAY-app-content-list" lay-filter="LAY-app-content-list" ></table>
			<!-- 记录层级-->
			<input type="hidden" id="a1" value=""/>
			<input type="hidden" id="a2" value=""/>
<!--			<script type="text/html" id="barDemo">-->
<!--				<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>-->
<!--				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
<!--			</script>-->
		</div>
	</div>
</div>
  <script>
  layui.config({
    base: '/public/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'contlist', 'table'], function(){
    var table = layui.table
    ,form = layui.form;

      table.render({
          elem: '#LAY-app-content-list'
          , url: '/admin/City/index_list'
          , title: '城市列表'
          ,toolbar: '#toolbarDemo'
          , totalRow: true
          , page: true
          ,cols: [[
              {field:'id', title:'ID',  fixed: 'left', unresize: true, sort: true}
              ,{field:'name', title:'名称'}
              ,{field:'son', title:'子地区',toolbar: '#barDemo2'}
              ,{field:'level', title:'等级'}
              ,{field:'pinyin', title:'拼音'}
              ,{field:'first', title:'首字母'}
              ,{field:'adcode', title:'地区编码'}
              ,{field:'lng', title:'经度'}
              ,{field:'lat', title:'纬度'}
              // ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
          ]]
      });

      var $ = layui.$, active = {
          reload: function () {
              var name = $('#name').val();
              //执行重载
              table.reload('LAY-app-content-list', {
                  page: {
                      curr: 1 //重新从第 1 页开始
                  }
                  , where: {
                      name: name,
                  }
              });
          }
      };

      //工具栏事件
      table.on('toolbar(LAY-app-content-list)', function(obj){
          var pid='';
          if($('#a2').val()!='')
		  {
              pid=$('#a2').val();
              $('#a2').val('');
		  }
          else
		  {
              pid=$('#a1').val();
              $('#a1').val('');
		  }
          switch(obj.event){
              case 'add':
                  layer.open({
                      type: 2
                      ,title: '添加城市'
                      ,content: '/admin/City/add_list'
                      ,maxmin: true
                      ,area: ['95%', '95%']
                  });
                  break;
              case 'history':
                  //执行重载
                  table.reload('LAY-app-content-list', {
                      page: {
                          curr: 1 //重新从第 1 页开始
                      },
                      where: {
                          name:'',
                          pid:pid,
                          // level:obj.config.where.level//市级   1是省级，2是市级，3是区级
                      }
                  });
                  $('#back').show();
                  if($('#a1').val()=='')
				  {
                      $('#back').hide();
				  }
                  break;
          };
      });
      //监听行工具事件
      table.on('tool(LAY-app-content-list)', function(obj){
          $('#a'+obj.data.level).val(obj.data.id);
		  if(obj.event === 'son'){
			  //执行重载
			  table.reload('LAY-app-content-list', {
				  page: {
					  curr: 1 //重新从第 1 页开始
				  },
				  where: {
				      pid:'',
				      name:'',
				      id:obj.data.id,
					  // level:parseInt(obj.data.level)//市级   1是省级，2是市级，3是区级
				  }
			  });
		  }
          $('#back').show();
      });

    $('.layui-card .layui-btn').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });


  });
  </script>
</body>
</html>
